
/*
    Software License

    Copyright (C) 2021-05-24  Xoronos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

/*
    Liabilities

    The software is provided "AS IS" without any warranty of any kind, either expressed,
    implied, or statutory, including, but not limited to, any warranty that the software
    will conform to specifications, any implied warranties of merchantability, fitness
    for a particular purpose, and freedom from infringement, and any warranty that the
    documentation will conform to the software, or any warranty that the software will
    be error free.

    In no event shall Xoronos be liable for any damages, including, but not limited to,
    direct, indirect, special or consequential damages, arising out of, resulting from,
    or in any way connected with this software, whether or not based upon warranty,
    contract, tort, or otherwise, whether or not injury was sustained by persons or
    property or otherwise, and whether or not loss was sustained from, or arose out of
    the results of, or use of, the software or services provided hereunder.

    To request the provided software under a different license you can contact us at
    support@xoronos.com
*/

#include <stdio.h>
#include <string.h>
#include "examples.h"
#include "xrn_crypto.h"
#include "xrn_core.h"

    /////////////////////////////////////////////////////
    //                   Introduction                  //
    /////////////////////////////////////////////////////
    // This code example shows how to generate         //
    // monomial and binomial keys.                     //
    // Monomial keys are used to store the distance    //
    // between two points. Binomial keys are used to   //
    // store the two points (start and end).           //
    // In literature monomial and binomial keys are    //
    // also refered to as private and public keys,     //
    // symmetric and asymmetric keys, decryption and   //
    // encryption keys.                                //
    /////////////////////////////////////////////////////

    /////////////////////////////////////////////////////
    //                   Pre-requisits                 //
    /////////////////////////////////////////////////////
    // The Xoron matrix decompressed data structure    //
    // needs to be mined before running this code      //
    // example. Some examples on how to mine Xoron     //
    // matrices can be found in                        //
    // generate_xrn_matrix_all_rnd.c or                //
    // generate_xrn_matrix_one_third.c                 //
    /////////////////////////////////////////////////////

int generate_keys_with_wrapper_functions(
 )
{

    //////////////////////////
    // Variable definitions //
    //////////////////////////

    // classical keys

    FILE *xstart_point_fp;
    FILE *xmonomial_key_fp;
    FILE *xbinomial_key_fp;

    // quantum keys

    FILE *polyvalent_key_fp;
    FILE *polyvalent_proof_fp;
    FILE *monovalent_key_fp;
    uint64_t start_timestamp;
    uint64_t end_timestamp;

    FILE *decompressed_xrn_matrix_fp;

    // library run time settings

    xrn_crypto_extra_settings_t settings;

    // all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt

    /////////////
    // Logging //
    /////////////

    // Set the default logging file streams
    // errors -> stderr
    // warnings -> stdout
    // notifications -> stdout
    // debug -> stdout

    xrn_set_default_log(  );

    // To change the default logging you can use the functions below,
    // where fp is the opened file pointer
    // 
    // xrn_change_error_log( fp )
    // xrn_change_warning_log( fp )
    // xrn_change_notification_log( fp )
    // xrn_change_debug_log( fp )

    /////////////////////////////
    // Variable initialization //
    /////////////////////////////

    // Initialize default run time settings
    // In this code example only the settings.rnd_str is used
    // In the xrn_load_default_settings function xrn_make_start_point string is used.
    // Other functions names can be also used to initialize settings

    if ( XSUCCESS != xrn_load_default_settings( &settings ) ) {
        return -1;
    }

    // To affect the randomness you can change the default string as
    // strcpy(settings.rnd_str,"my_string");

    //////////////////////////
    // Make the start point //
    //////////////////////////

    printf( "making start point ...\n" );

    xstart_point_fp = fopen( "xstart_point.xf", "wb" );
    decompressed_xrn_matrix_fp = fopen( "xrn_matrix.xm", "rb" );
    if ( ( decompressed_xrn_matrix_fp == NULL ) || ( xstart_point_fp == NULL ) ) {
        return -1;
    }

    if ( XSUCCESS != xrn_make_start_point_wrapper( xstart_point_fp, decompressed_xrn_matrix_fp, settings ) ) {
        fclose( xstart_point_fp );
        fclose( decompressed_xrn_matrix_fp );
        return -1;
    }

    // An alternative to the xrn_make_start_point_wrapper is the xrn_make_start_point function
    // if ( XSUCCESS != xrn_make_start_point( xstart_point, xrn_matrix settings ) ) {  
    // where xstart_point is of the type ( xstart_point_t * )
    // where xrn_matrix is of the type ( xrn_matrix_t * )

    if ( ( 0 != fclose( xstart_point_fp ) ) || ( 0 != fclose( decompressed_xrn_matrix_fp ) ) ) {
        return -1;
    }

    /////////////////////////////////////
    // Make xmonomial_key ( distance ) //
    /////////////////////////////////////

    printf( "making monomial key ...\n" );

    xmonomial_key_fp = fopen( "xmonomial_key.xf", "wb" );
    if ( xmonomial_key_fp == NULL ) {
        return -1;
    }

    if ( XSUCCESS != xrn_make_monomial_key_wrapper( xmonomial_key_fp, settings ) ) {
        fclose( xmonomial_key_fp );
        return -1;
    }

    // An alternative to the xrn_make_monomial_key_wrapper is the xrn_make_monomial_key function
    // if ( XSUCCESS != xrn_make_monomial_key( xmonomial_key, settings ) ) {  
    // where xmonomial_key is of the type ( xmonomial_key_t * )

    if ( 0 != fclose( xmonomial_key_fp ) ) {
        return -1;
    }

    ///////////////////////////////////////
    // Make xbinomial_key ( start, end ) //
    ///////////////////////////////////////

    printf( "making binomial key ...\n" );

    xstart_point_fp = fopen( "xstart_point.xf", "rb" );
    xmonomial_key_fp = fopen( "xmonomial_key.xf", "rb" );
    decompressed_xrn_matrix_fp = fopen( "xrn_matrix.xm", "rb" );
    xbinomial_key_fp = fopen( "xbinomial_key.xf", "wb" );

    if ( ( xstart_point_fp == NULL ) || ( xmonomial_key_fp == NULL )
         || ( decompressed_xrn_matrix_fp == NULL ) || ( xbinomial_key_fp == NULL ) ) {
        return -1;
    }

    if ( XSUCCESS != xrn_make_binomial_key_wrapper( decompressed_xrn_matrix_fp, // the Xoron matrix file pointer
                                                    xstart_point_fp,    // the start point file pointer
                                                    xmonomial_key_fp,   // the monomial key file pointer
                                                    xbinomial_key_fp,   // the binomial key file pointer
                                                    settings ) ) {
        fclose( xbinomial_key_fp );
        fclose( decompressed_xrn_matrix_fp );
        fclose( xmonomial_key_fp );
        fclose( xstart_point_fp );
        return -1;
    }

    // An alternative to the xrn_make_binomial_key_wrapper is the xrn_make_binomial_key function
    // if ( XSUCCESS !=
    //      xrn_make_binomial_key( xrn_matrix, // the xrn_matrix is of the type ( xrn_matrix_t * )
    //                          xstart_point,  // the xstart_point is of the type ( xstart_point_t )
    //                          xmonomial_key, // the monomial_key is of the type ( xmonomial_key_t )
    //                          xbinomial_key  // the binomial_key is of the type ( xbinomial_key_t * )
    //                        ) ) {

    if ( ( 0 != fclose( xmonomial_key_fp ) ) || ( 0 != fclose( xbinomial_key_fp ) )
         || ( 0 != fclose( decompressed_xrn_matrix_fp ) ) || ( 0 != fclose( xstart_point_fp ) ) ) {
        return -1;
    }

    /////////////////////////////////////////
    // Make xmonovalent key ( start, end ) //
    /////////////////////////////////////////

    printf( "making monovalent and polyvalent key, and polyvalant proof...\n" );

    // create a always valid key

    start_timestamp = 0;
    end_timestamp = 0xffffffffffffffff;

    decompressed_xrn_matrix_fp = fopen( "xrn_matrix.xm", "rb" );
    polyvalent_key_fp = fopen( "xpolyvalent_key.xf", "wb" );
    polyvalent_proof_fp = fopen( "xpolyvalent_proof.xf", "wb" );
    monovalent_key_fp = fopen( "xmonovalent_key.xf", "wb" );

    if ( XSUCCESS != xrn_make_monovalent_key_wrapper( decompressed_xrn_matrix_fp,       // the Xoron matrix file pointer
                                                      start_timestamp,  // start of validity in unix time
                                                      end_timestamp,    // end of validity in unix time 
                                                      polyvalent_key_fp,        // the polyvalent key (secret key)
                                                      polyvalent_proof_fp,      // the polyvalent proof (secret proof of having generated the monovalent key)
                                                      monovalent_key_fp,        // the monovalent key (key that can be disclosed)
                                                      settings ) ) {
        fclose( decompressed_xrn_matrix_fp );
        fclose( polyvalent_key_fp );
        fclose( polyvalent_proof_fp );
        fclose( monovalent_key_fp );
        return -1;
    }

    // An alternative to the xrn_make_monovalent_key_wrapper is the xrn_make_monovalent_key function
    // if ( XSUCCESS !=
    //      xrn_make_monovalent_key ( xrn_matrix, // the xrn_matrix is of the type ( xrn_matrix_t * )
    //                             start_timestamp,     // start of validity in unix time
    //                             end_timestamp,       // end of validity in unix time 
    //                             xpolyvalent_key,  // the polyvalent key is of the type ( xpolyvalent_key_t )
    //                             xpolyvalent_proof, // the polyvalent proof is of the type ( xpolyvalent_proof_t )
    //                             xmonomial_key  // the xmonomial_key is of the type ( xmonovalent_key_t * )
    //                             settings  // the run time settings
    //                        ) ) {

    if ( ( 0 != fclose( polyvalent_key_fp ) ) || ( 0 != fclose( polyvalent_proof_fp ) )
         || ( 0 != fclose( decompressed_xrn_matrix_fp ) ) || ( 0 != fclose( monovalent_key_fp ) ) ) {
        return -1;
    }

    printf( "keys successfully created\n" );
    return 0;

}

